import pandas, duckdb
from time import strftime
import os
from utils.path_utils import get_project_root
from database.duckdb.duckdb_base import get_connection



# timenow = str(strftime("%Y%m%d%H%M%S"))
def run(dwbh, dwid, kmtx_id, zb, kjnd, timenow):
  conn = get_connection()

  conn.execute("""
               delete from T_API_KM where fiyear = ? and LEDGERBH = ?;
               """, [kjnd, zb])
  conn.execute("""
    insert into T_API_KM (ID, CHARTOFACCBH, CHARTOFACCNAME, ACCTITLEPROPBH, ACCTITLEPROP, CODE, NAME_CHS, FULLNAME_CHSCL, FULLNAME_CHS, BALANCEDIR, ISBANKBOOK, 
               ISDISABLED, esun_api_update, accorgid, accorgbh, fiyear, LEDGERBH, chartofacc)
              select
                gsdm||'-'||kmdm,
                '科目体系编号',
                '科目体系名称',
                Kmmxlb,
                Kmmxlb,
                Kmdm,
                Kmmc,
                '科目全称策略',
                Kmmc,
                case Yefx
                when 'J' then '1'
                when 'D' then '2'
                end,
                '银行账',
                Syzt,
                '"""+timenow+"""',
                '"""+dwid+"""',
                '"""+dwbh+"""',
                '"""+kjnd+"""',
                '"""+zb+"""',
                '"""+kmtx_id+"""'
               from gl_kmxx;
  """)

  conn.close()